package com.threadpool.mythread.case01;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.TimeUnit;

/**
 * @Author 李李李
 * @2020/4/26 20:27
 *
 * ArrayBlockingQueue 队列
 *
 */
public class Case09 {
    public static void main(String[] args) throws InterruptedException {
//        api1();
//        api2();
//        api3();
        api4();
    }


    /**
     * 会报错
     */
    public static void api1(){
        ArrayBlockingQueue<String> arrayBlockingQueue = new ArrayBlockingQueue<>(3);
        System.out.println(arrayBlockingQueue.add("a"));
        System.out.println(arrayBlockingQueue.add("b"));
        System.out.println(arrayBlockingQueue.add("c"));
//        System.out.println(arrayBlockingQueue.add("d"));

        //获取队列首元素
        System.out.println(arrayBlockingQueue.element());

        System.out.println(arrayBlockingQueue.remove());
        System.out.println(arrayBlockingQueue.remove());
        System.out.println(arrayBlockingQueue.remove());
//        System.out.println(arrayBlockingQueue.remove());
    }


    /**
     * 不会报错 不会抛出异常
     */
    public static void api2(){
        ArrayBlockingQueue<String> arrayBlockingQueue = new ArrayBlockingQueue<>(3);
        System.out.println(arrayBlockingQueue.offer("a"));
        System.out.println(arrayBlockingQueue.offer("b"));
        System.out.println(arrayBlockingQueue.offer("c"));
        System.out.println(arrayBlockingQueue.offer("d"));

        //监测队首元素
        System.out.println( arrayBlockingQueue.peek());

        System.out.println(arrayBlockingQueue.poll());
        System.out.println(arrayBlockingQueue.poll());
        System.out.println(arrayBlockingQueue.poll());
        System.out.println(arrayBlockingQueue.poll());
    }

    /**
     * 一直阻塞等待
     */
    public static void api3() throws InterruptedException {
        ArrayBlockingQueue<String> arrayBlockingQueue = new ArrayBlockingQueue<>(3);
        arrayBlockingQueue.put("a");
        arrayBlockingQueue.put("b");
        arrayBlockingQueue.put("c");
//        arrayBlockingQueue.put("d");

        System.out.println(arrayBlockingQueue.take());
        System.out.println(arrayBlockingQueue.take());
        System.out.println(arrayBlockingQueue.take());
        System.out.println(arrayBlockingQueue.take());
    }


    /**
     * 不会报错 不会抛出异常
     */
    public static void api4() throws InterruptedException {
        ArrayBlockingQueue<String> arrayBlockingQueue = new ArrayBlockingQueue<>(3);
        System.out.println(arrayBlockingQueue.offer("a",2, TimeUnit.SECONDS));
        System.out.println(arrayBlockingQueue.offer("b",2, TimeUnit.SECONDS));
        System.out.println(arrayBlockingQueue.offer("c",2, TimeUnit.SECONDS));
        System.out.println(arrayBlockingQueue.offer("d",2, TimeUnit.SECONDS));

        //监测队首元素
        System.out.println( arrayBlockingQueue.peek());

        System.out.println(arrayBlockingQueue.poll(1,TimeUnit.SECONDS));
        System.out.println(arrayBlockingQueue.poll(1,TimeUnit.SECONDS));
        System.out.println(arrayBlockingQueue.poll(1,TimeUnit.SECONDS));
        System.out.println(arrayBlockingQueue.poll(1,TimeUnit.SECONDS));
    }
}
